﻿Imports System.Data.OleDb
Public Class Videos
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If IsPostBack = False Then

            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
            conn.Open()
            Dim LikedorNotsql As String = "SELECT * FROM CoinVideoLike WHERE Videoid = @f1 AND VideoViewer = @f2"
            Dim LikedorNotcmd As New OleDbCommand(LikedorNotsql, conn)
            LikedorNotcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            LikedorNotcmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            Dim dr As OleDbDataReader = LikedorNotcmd.ExecuteReader()
            If (dr.HasRows()) Then
                Btn_Videolike.Visible = False
                lbl_Videolike.Visible = True
                img_Videolike.Visible = True
            Else
                Btn_Videolike.Visible = True
                lbl_Videolike.Visible = False
                img_Videolike.Visible = False
            End If
        End If
        Dim LikeCountsql As String = "SELECT * FROM CoinVideoLikeCount WHERE Videoid=@f30"
        Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        conn2.Open()
        Dim LikeCountcmd As OleDbCommand = New OleDbCommand(LikeCountsql, conn2)
        LikeCountcmd.Parameters.AddWithValue("@f30", Request.Params("ID"))
        Dim datareader = LikeCountcmd.ExecuteReader()
        datareader.Read()
        If (datareader.HasRows) Then
            lbl_VideolikeCounts.Text = datareader("UserVideoLikes")
        Else
            lbl_VideolikeCounts.Text = 0
        End If

        Dim Ratingconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        Dim Ratings As Single = 0
        If Not IsPostBack Then
            BindAverageRatingControl()
        End If

        Dim VideoViewssql As String = "INSERT INTO VideoView (VideoID,Viewer) VALUES (@f1,@f2)"
        Dim VideoViewsconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)

        VideoViewsconn.Open()

        Dim VideoViewscmd As OleDbCommand = New OleDbCommand(VideoViewssql, VideoViewsconn)

        VideoViewscmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        VideoViewscmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        VideoViewscmd.ExecuteNonQuery()
        VideoViewsconn.Close()
        VideoViewscmd.Dispose()
        VideoViewsconn.Dispose()

        Dim VideoViewCountsql As String = "SELECT * FROM VideoViewQuery WHERE VideoID=@f20"
        Dim VideoViewCountconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        VideoViewCountconn.Open()

        Dim VideoViewCountcmd As OleDbCommand = New OleDbCommand(VideoViewCountsql, VideoViewCountconn)

        VideoViewCountcmd.Parameters.AddWithValue("@f20", Request.Params("id"))
        Dim datareader2 = VideoViewCountcmd.ExecuteReader()
        datareader2.Read()
        If (datareader2.HasRows) Then
            lbl_VideoViews.Text = datareader2("CountOfViews")
        Else
            lbl_VideoViews.Text = 0
        End If
    End Sub

    Protected Sub Btn_Videolike_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Videolike.Click

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        conn.Open()
        Dim Videolikesql As String = "INSERT INTO CoinVideoLike (Videoid,VideoViewer) VALUES (@f1,@f2)"
        Dim Videolikecmd As New OleDbCommand(Videolikesql, conn)

        Videolikecmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Videolikecmd.Parameters.AddWithValue("@f2", User.Identity.Name)



        Videolikecmd.ExecuteNonQuery()
        Btn_Videolike.Visible = False
        lbl_Videolike.Visible = True
        img_Videolike.Visible = True
        conn.Close()


        Response.Redirect("Videos.aspx?ID=" & Request.QueryString("ID"))
    End Sub
    Public Sub RatingControlChanged(ByVal sender As Object, ByVal e As AjaxControlToolkit.RatingEventArgs)
        Dim CoinID = Request.Params("ID")
        Dim Ratingsconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("Coinsconn").ConnectionString)
        Dim RatingsVideosql As String = "DELETE FROM UserVideoRating WHERE Rater=@f1 and CoinVideofkID=@f2"
        Dim RatingVideoCmd As New OleDbCommand(RatingsVideosql, Ratingsconn)
        Ratingsconn.Open()
        RatingVideoCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        RatingVideoCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        RatingVideoCmd.ExecuteNonQuery()
        Ratingsconn.Close()
        RatingsVideosql = "INSERT INTO UserVideoRating(Rating,Rater,CoinVideofkID) VALUES (@f1,@f2,@f3)"
        Dim addRatingCmd As New OleDbCommand(RatingsVideosql, Ratingsconn)
        Ratingsconn.Open()
        addRatingCmd.Parameters.AddWithValue("@f1", Ratn_VideoStar.CurrentRating)
        addRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addRatingCmd.Parameters.AddWithValue("@f3", Request.Params("ID"))
        addRatingCmd.ExecuteNonQuery()
        Ratingsconn.Close()
    End Sub

    Public Sub BindAverageRatingControl()
        Dim Ratingconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        Dim CoinID = Request.Params("ID")
        Dim Ratings As Single = 0
        Dim DtRating As DataTable = New DataTable()
        Ratingconn.Open()
        Dim RatingVideocmd As OleDbCommand = New OleDbCommand("SELECT UserVideoRating.CoinVideofkID, AVG(Rating) AS AverageVideoRating FROM(UserVideoRating) WHERE CoinVideofkID =" & CoinID & "  GROUP BY CoinVideofkID", Ratingconn)
        Dim DataAdap As OleDbDataAdapter = New OleDbDataAdapter(RatingVideocmd)
        DataAdap.Fill(DtRating)

        If DtRating.Rows.Count <> 0 Then
            Ratings = Single.Parse(DtRating.Rows(0)("AverageVideoRating").ToString())
            Ratn_Videoavg.CurrentRating = Convert.ToInt32(Ratings)
        Else
            Ratn_Videoavg.CurrentRating = 0
        End If
        lbl_MyVideoRating.Text = Ratings.ToString()

    End Sub
End Class